package cn.itcast.goods.user.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.sun.org.apache.bcel.internal.generic.NEW;

import cn.itcast.goods.user.domain.User;
import cn.itcast.jdbc.TxQueryRunner;

/**
 * 用户模块持久层
 * @author GiantTao
 *
 */
public class UserDao {
	private QueryRunner qr=new TxQueryRunner();
	
	/**
	 * 根据用户名修改密码
	 * @param uid
	 * @param newPass
	 * @throws SQLException
	 */
	public void updatePassword(String uid,String newPass) throws SQLException{
		String sql="update t_user set loginpass=? where uid=?";
		qr.update(sql,newPass,uid);
	}
	/**
	 * 根据用户名和密码查询用户
	 * @param uid
	 * @param loginpass
	 * @return
	 * @throws SQLException
	 */
	public boolean getByUidAndPass(String uid,String loginpass) throws SQLException{
		String sql="select count(*) from t_user where uid=? and loginpass=?";
		Number number=(Number)qr.query(sql, new ScalarHandler(),uid,loginpass);
		return number.intValue()>0;
		
	}
	/**
	 * 根据用户名和密码进行数据库查询 
	 * @param loginname
	 * @param loginpass
	 * @return
	 * @throws SQLException
	 */
	public User getUserByLoginnameAndLoginpass(String loginname,String loginpass) throws SQLException{
		String sql="select * from t_user where loginname=? and loginpass=? ";
		return qr.query(sql, new BeanHandler<User>(User.class),loginname,loginpass);
	}
	/**
	 * 修改用户状态
	 * @param uid
	 * @param status
	 * @throws SQLException
	 */
	public void updateStatus(String uid,int status) throws SQLException{
		String sql="update t_user set status=? where uid=?";
		qr.update(sql,status,uid);
	}
	/**
	 * 通过激活码获取用户信息
	 * @param code
	 * @return
	 * @throws SQLException
	 */
	public User getUserByActivationCode(String code) throws SQLException {
		String sql="select * from t_user where activationCode=?";
		return qr.query(sql, new BeanHandler<User>(User.class),code);
	}
	/**
	 * 检测用户名是否注册
	 * @param loginname
	 * @return
	 * @throws SQLException
	 * 由于查询的结果是单行单列数据，因而对结果的处理使用ScalarHandler
	 * 如果返回的结果是一个对象，则使用ResultSetHandler
	 * 如果返回的结果是多个对象，则使用MapHandler()
	 */
	public boolean ajaxValidateLoginname(String loginname) throws SQLException{
		String sql="select count(*) from t_user where loginname=?";
		Number number=(Number) qr.query(sql,new ScalarHandler(), loginname);
		return number.intValue()==0;
	}
	
	/**
	 * 检测邮箱是否注册
	 * @param email
	 * @return
	 * @throws SQLException
	 */
	public boolean ajaxValidateEmail(String email) throws SQLException{
		String sql="select count(*) from t_user where email=?";
		Number number=(Number) qr.query(sql,new ScalarHandler(), email);
		return number.intValue()==0;
	}
	
	/**
	 * 向数据库写入用户信息
	 * @param user
	 * @throws SQLException
	 */
	public void add(User user) throws SQLException{
		String sql="insert into t_user values(?,?,?,?,?,?)";
		Object params[]={user.getUid(),user.getLoginname(),user.getLoginpass(),
				user.getEmail(),user.getStatus(),user.getActivationCode()};
		qr.update(sql, params);
	}
}
